#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
  echo -e "${ERROR} RALAT ${RESET}${TEXT} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

function installPkg {
  read -p "Teruskan dengan pemasangan semula? [Y/n] " _yesno
  if [[ "$_yesno" =~ ^[Yy]$ ]]; then
    if [[ ! -f /etc/stunnel/stunnel.conf ]]; then
      wget -q -O - https://raw.githubusercontent.com/cybertize/debian/buster/packages/stunnel.sh | bash
    fi
    apt-get -y -qq --purge remove stunnel &>/dev/null
    apt-get -y -qq autoremove &>/dev/null
    apt-get -qq autoclean &>/dev/null
    clear
    sleep 3
    wget -q -O - https://raw.githubusercontent.com/cybertize/debian/buster/packages/stunnel.sh | bash
  else
    echo -e "${ERROR} RALAT ${RESET}${TEXT} Membatalkan pemasangan semula ${RESET}"
    exit 1
  fi
}

function uninstallPkg {
  read -p "Adakah anda pasti mahu menyahpasang stunnel4? [Y/n] " _yesno
  if [[ "$_yesno" =~ ^[Yy]$ ]]; then
    {
      apt-get -y -qq --purge remove stunnel*
      apt-get -y -qq autoremove
      apt-get -y -qq autoclean
    } &>/dev/null

    echo -e "${SUCCESS} INFO ${RESET}${TEXT} Bersihkan & Keluarkan pakej stunnel ${RESET}"
    exit 0
  else
    echo -e "${ERROR} INFO ${RESET}${TEXT} Batalkan penyahpasangan pakej ${RESET}"
    exit 1
  fi
}

function configurePkg {
  local stunnel_ports=($(cat /etc/stunnel/stunnel.conf | grep -E 'accept = ' | cut -d ' ' -f 3))

  echo
  echo -e "Secara lalai perkhidmatan stunnel menggunakan 3 port $stunnel_ports"
  echo
  while true; do
    read -r -p "Adakah anda mahu ganti port baharu? [Y/n] " __yesno
    if [[ "$__yesno" =~ ^[Yy]$ ]]; then
      read -p "Masukkan port baru: " old_dropbear_port old_openvpn_port old_sslibev_port
      if [[ -n "$old_dropbear_port" && "$old_dropbear_port" =~ ^[0-9]+$ ]]; then
        sed -i 's|${stunnel_ports[0]}|$old_dropbear_port|g' /etc/stunnel/stunnel.conf
      fi
      if [[ -n "$old_openvpn_port" && "$old_openvpn_port" =~ ^[0-9]+$ ]]; then
        sed -i 's|${stunnel_ports[1]}|$old_openvpn_port|g' /etc/stunnel/stunnel.conf
      fi
      if [[ -n "$old_sslibev_port" && "$old_sslibev_port" =~ ^[0-9]+$ ]]; then
        sed -i 's|${stunnel_ports[2]}|$old_sslibev_port|g' /etc/stunnel/stunnel.conf
      fi

      echo -e "${INFO} INFO ${RESET}${TEXT} Perubahan telah dibuat ${RESET}"
      break
    else
      echo -e "${INFO} INFO ${RESET}${TEXT} Tiada perubahan dibuat ${RESET}"
      break
    fi
  done
}

function detailPkg {
  local unit_name=$(systemctl show stunnel4.service -p Names | cut -d '=' -f 2)
  local unit_desc=$(systemctl show stunnel4.service -p Description | cut -d '=' -f 2)
  local is_active=$(systemctl is-active stunnel4.service)
  local is_enable=$(systemctl is-enabled stunnel4.service)

  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} Butiran perkhidmatan Stunnel4                              ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "${YELLOW}   Name:${RESET} ${GREEN}$unit_name${RESET}"
  echo -e "${YELLOW}   Desc:${RESET} ${GREEN}$unit_desc${RESET}"
  echo -e "${YELLOW} Status:${RESET} ${GREEN}$is_active & $is_enable${RESET}"
  echo -e "${YELLOW}  Ports:${RESET} ${GREEN}$stunnel_ports${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
}

function stunnelMenu {
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} MENU STUNNEL                                               ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "[01] ${GREEN}reinstall${RESET}    - ${YELLOW}Reinstall stunnel package${RESET}"
  echo -e "[02] ${GREEN}uninstall${RESET}    - ${YELLOW}Uninstall stunnel package${RESET}"
  echo -e "[03] ${GREEN}configure${RESET}    - ${YELLOW}Configure stunnel package${RESET}"
  echo -e "[04] ${GREEN}detail   ${RESET}    - ${YELLOW}Show stunnel detail & status${RESET}"
  echo -e "[00] ${GREEN}quit     ${RESET}    - ${YELLOW}Exit from menu${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
  while true; do
    read -p "Masukkan pilihan anda: " _choice
    case "$_choice" in
    01 | reinstall)
      installPkg && break
      ;;
    02 | uninstall)
      uninstallPkg && break
      ;;
    03 | configure)
      configurePkg && break
      ;;
    04 | detail)
      detailPkg && break
      ;;
    00 | quit)
      exit 0
      ;;
    *)
      echo -e "${ERROR} RALAT ${RESET}${TEXT} Pilihan tidak sah ${RESET}"
      stunnelMenu
      ;;
    esac
  done
}

if [[ -f /etc/stunnel/stunnel.conf ]]; then
  stunnelMenu
elif [[ ! -f /etc/stunnel/stunnel.conf ]]; then
  installPkg
fi
